#include<avr/io.h>
#include<avr/interrupt.h>
#include"timer.h"


type_pointer_handler handler1;
type_pointer_handler handler2;
type_pointer_handler handler3;

short count1; 
short count2; 
short count3; 

short count_cmp1; 
short count_cmp2; 
short count_cmp3; 

void Init_Timer0(void)
{
 count_cmp1=-1;
 count_cmp2=-1;
 count1=0;
 count2=0;
 OCR0=16;
 TCCR0|=1<<WGM01|1<<CS02|1<<CS00;
 TIMSK|=1<<OCIE0;
};

void set_handler1(unsigned char period,type_handler f)
{
	count_cmp1=period;
	handler1=f;
};

void set_handler2(unsigned char period,type_handler f)
{
	count_cmp2=period;
	handler2=f;
};

void set_handler3(unsigned char period,type_handler f)
{
	count_cmp3=period;
	handler3=f;
};

void Init_Timer1(short init)
 {
  DDRB|=1<<PB7;
  ICR1=init;
  OCR1C=init/2;
  TCCR1A|=1<<WGM11|1<<COM1C1|1<<COM1C0;
  TCCR1B|=1<<CS10|1<<WGM13|1<<WGM12;
 };

void Init_Timer2(void)
 {
	TIMSK|=1<<TOIE2;
	TCCR2|=1<<CS22|0<<CS21|1<<CS20;
 };



SIGNAL(TIMER0_COMP_vect)
{
//sei();
	if (count_cmp1!=-1)
	 {
	  if (count_cmp1==count1) 
	   {
	    (*handler1)(); 
		 count1=0;
	   } else count1++;
	 }
	if (count_cmp2!=-1)
	 {
	  if (count_cmp2==count2) 
	   {
	    (*handler2)(); 
		count2=0;
	   } else count2++;
	 }
};

SIGNAL(TIMER2_OVF_vect)
{
	if (count_cmp3!=-1)
	 {
	  if (count_cmp3==count3) 
	   {
	    (*handler3)(); 
		count3=0;
	   } else count3++;
	 }
};
